home *** CD-ROM | disk | FTP | other *** search
-
- { DATARAN Corp. Dec 7 87 AMZ
- numeric formatting functions
-
-
- functions include FMTSTR(fmt,str);
- FMTINT(fmt,int);
- FMTREAL(fmt,real);
- FMTLONG(fmt, long)
- fmtbool(fmt,bool);
- fmtword(fmt,word);
-
- All functions are based on FMTSTR, a general purpose string formatter.
- The format string contains control characters that tell fmtstr what to do.
- Available format controls are:
- , Put nice commas in the formatted number
- $ Put a dollar sign at front of number
- Dn Format number as a string with n decimal points, padding if reqd.
- , Place commas into proper place or left of decimal point( if any)
- Rnn Place string right justified in a field nn long.
- Lnn Place string left justified in a fiels nn wide.
- Cnn Center string in a field nn wide.
-
- }
- unit printf;
-
- interface
- uses testlib, tpstring;
-
- function fmtstr(fstr: med_string; vstr : anystring) : anystring; { string formatter }
-
- function fmtint(fstr : med_string; number: integer) : anystring; { integer}
- function fmtlong(fstr : med_string; number: longint) : anystring; { integer}
- function fmtword(fstr : med_string; number: word) : anystring; { integer}
- function fmtbool(fstr : med_string; bool: boolean): anystring;
-
- function fmtreal(fstr : med_string; number: real) : anystring;
-
- implementation
-
- function fmtstr;
-
- var
- tempstr : maxstring;
- tempfmt : string[10];
- len, dot,digits,i,commas : integer; { locals }
- code, places,original : integer;
- zeroes : string[10];
-
- begin { format function start }
- zeroes := '0000000000';
- tempstr := trim(vstr); { pick up the variable string without spaces}
- tempfmt := stupcase(fstr); { upper case format strint }
- len := length(tempstr); { length of packed string }
-
-
- if pos(',',tempfmt) <> 0 then begin { comma insertion }
- dot := pos('.',tempstr); { location of a decimal if any }
-
- if dot = 0 then dot := len+1 ; { dot is 1 more than we fool with }
- if copy(tempstr,1,1) <> '-' then
- commas := (dot-2) div 3 { numnber of commas needed }
- else { if a leading - present }
- commas := (dot-3) div 3;
-
- if commas <> 0 then begin; { at least 1 comma needed }
- for i := 1 to commas do
- insert(',' , tempstr, dot-(i*3) );
- end; { non zero number of commas needed }
- end; { comma routine }
-
- if pos('$',tempfmt) <> 0 then tempstr := '$'+tempstr; { add in bucks }
-
- (*
- { ----- DECIMAL PLACE ADJUSTMENT -------}
-
- if pos('D',tempfmt) <> 0 then begin { wants to specify decimal places }
- dot := pos('.', tempstr); { location of existing . }
- i := pos('D',tempfmt)+1;
- val(copy(tempfmt,i,1),places,code); { number of places }
-
- if code = 0 then begin { valid number of decimals}
- if (places = 0) and (dot <> 0) then { needs decimals removed }
- tempstr := copy(tempstr,1,dot-1);
-
- if (places <> 0) and (dot = 0) then
- tempstr := tempstr + '.'; { add a . to start with }
-
- if (places <> 0) then begin { make places correct }
- dot := pos('.',tempstr); { find out where . now is }
- original := length( copy(tempstr,dot+1,50)); { orignal places }
- if original < places then { pad with zeroes }
- tempstr := tempstr + copy(zeroes,1, places-original);
- if original > places then begin { truncate fraction }
- len := length(tempstr);
- tempstr := copy(tempstr,1, len-(original-places));
- end;
- end; { desired places <> 0 }
- end; { valid number of places desired }
- end; { decimal palace correction desired }
- *)
-
- { LEFT RIGHT AND CENTER PROCEDURE }
-
- if pos('L', tempfmt) <> 0 then begin { left justify desired }
- dot := pos('L',tempfmt); { location of control }
- val( copy(tempfmt,dot+1, 2),places, code); { convert the width spec }
- tempstr := trim(tempstr);
- if length(tempstr) < places then
- tempstr := pad(tempstr , places ); { pad out}
- end;
-
- if pos('R', tempfmt) <> 0 then begin { right justify }
- dot := pos('R', tempfmt);
- val( copy(tempfmt, dot+1,2), places,code); { get wifth spec }
- tempstr := trim(tempstr);
- if length(tempstr) < places then
- tempstr := leftpad(tempstr, places );
- end;
-
- if pos('C', tempfmt) <> 0 then begin { center justify }
- dot := pos('C', tempfmt);
- val( copy(tempfmt, dot+1, 2), places,code); { get width }
- tempstr := trim(tempstr); { get rid of any possible spalces }
- if (length(tempstr) < places ) and (code = 0) then begin
- (*
- code := (places - length(tempstr)) div 2; { white space fore and aft}
- tempstr := bunch(' ',code) + tempstr + bunch(' ',code);
-
- if length(tempstr) < places then
- tempstr := tempstr + bunch(' ', places-length(tempstr));
- *)
- tempstr := center(tempstr,places);
- end;
- end; { end of center operation }
-
- fmtstr := tempstr; { assign to function variable }
-
- end; { end of format function }
-
-
- function fmtbool;
- var
- temp1 : string[30];
- begin
- if bool then temp1 := 'True' else temp1 := 'False'; {d efault}
- if pos( 'Y',stupcase(fstr)) <> 0 then begin
- if bool then temp1 := 'Yes' else temp1 := 'No';
- end;
- if pos( 'T',stupcase(fstr)) <> 0 then begin
- if bool then temp1 := 'True' else temp1 := 'False';
- end;
- if pos( 'N',stupcase(fstr)) <> 0 then begin
- if bool then temp1 := '1' else temp1 := '0';
- end;
- if pos( 'O',stupcase(fstr)) <> 0 then begin
- if bool then temp1 := 'On' else temp1 := 'Off';
- end;
-
-
- fmtbool := fmtstr(fstr,temp1) { use regular string converter }
- end;
-
-
- function fmtint;
- var
- temp1 : string[30];
- begin
- str(number,temp1); { create a string from integer }
- temp1 := trim(temp1); { get rid of all spaces }
- fmtint := fmtstr(fstr,temp1) { use regular string converter }
- end;
-
- function fmtlong;
- var
- temp1 : string[30];
- begin
- str(number,temp1); { create a string from integer }
- temp1 := trim(temp1); { get rid of all spaces }
- fmtlong := fmtstr(fstr,temp1) { use regular string converter }
- end;
-
- function fmtword(fstr : med_string; number: word) : anystring; { integer }
- var
- temp1 : string[30];
- begin
- str(number,temp1); { create a string from word }
- temp1 := trim(temp1); { get rid of all spaces }
- fmtword := fmtstr(fstr,temp1) { use regular string converter }
- end;
-
-
- function fmtreal(fstr : med_string; number: real) : anystring;
-
- var
- temp1 : string[30];
- tempfmt : small_string;
- dot : integer;
- places,code : integer;
-
- begin
- places := 0; { assume no decimals }
- tempfmt := stupcase(trim(fstr));
- dot := pos('D',tempfmt); { wants to specify decimal places }
- if dot <> 0 then begin
- val(copy(tempfmt,dot+1,1),places,code); { number of decimal places }
- end;
- str(number:24:places, temp1); { create a string from real}
- fmtreal := fmtstr(fstr,temp1) { use regular string converter for rest}
- end;
-
- begin
- end.